<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>计算属性 </title>
    <script src="/js/vue-2.7.9-dev.js"></script>
</head>
<body>
    <!--  使用插值语法 -->
    <!-- <div id="app">
        <div>
            <label>姓</label>
            <input type="text" v-model="lastName"/>
        </div>
        <div>
            <label>名</label>
            <input type="text" v-model="firstName"/>
        </div>
        <div>
            全名: {{lastName}}-{{firstName}}
        </div>
    </div>

    <script>
        let vm = new Vue({
            data() { 
                return { 
                    lastName:"",
                    firstName:"",
                }
            },
        })
        vm.$mount("#app")
    </script> -->

    <!-- 使用method 实现 -->
    <!-- <div id="app">
        <div>
            <label>姓</label>
            <input type="text" v-model="lastName"/>
        </div>
        <div>
            <label>名</label>
            <input type="text" v-model="firstName"/>
        </div>
        <div>
            全名: {{fullName()}}
        </div>
    </div>

    <script>
        let vm = new Vue({
            data() { 
                return { 
                    lastName:"",
                    firstName:"",
                }
            },
            methods: {
                fullName(){
                    console.log("fullname 执行了");
                    return this.lastName +"-" + this.firstName
                }
            },
        })
        vm.$mount("#app")
    </script> -->

    <!-- 使用计算属性实现 -->
    <div id="app">
        <div>
            <label>姓</label>
            <input type="text" v-model="lastName"/>
        </div>
        <div>
            <label>名</label>
            <input type="text" v-model="firstName"/>
        </div>
        <div>
            全名: {{fullName}}
        </div>
    </div>

    <script>
        let vm = new Vue({
            data() { 
                return { 
                    lastName:"",
                    firstName:"",
                }
            },
            computed:{
                // 全写
                // fullName: {
                //     get() {
                //         console.log('get被调用了')
                //         return this.firstName + '-' + this.lastName
                //     },
                //     set(value) {
                //         console.log('set', value)
                //         const arr = value.split('-')
                //         this.firstName = arr[0]
                //         this.lastName = arr[1]
                //     }
                // }

                // 简写
                fullName() {
                    console.log('get被调用了')
                    return this.firstName + '-' + this.lastName
                },
            }
        })
        vm.$mount("#app")
        console.log(vm);
        console.log("调用fullName getter",vm.fullName);
    </script>
</body>
</html>